package com.flyu.myThread.Syn;


/*
不安全的取钱
两个人去取钱
 */
public class UnsafeBank {
    public static void main(String[] args) {
        Account account = new Account(100, "结婚基金");
        Drawing you = new Drawing(account, 50, "你");
        Drawing girlFriend = new Drawing(account, 100, "girlFriend");

        you.start();
        girlFriend.start();
    }
}

// 账户
class Account{
    int money;  // 余额
    String name; // 卡名
    public Account(int money, String name){
        this.money = money;
        this.name = name;
    }
}

// 取钱
class Drawing extends Thread{
    // 账户
    Account account;
    // 取了多少
    int drawingMoney;
    // 手里多少
    int nowMoney;

    // 有参构造
    public Drawing(Account account, int drawingMoney, String name){
        super(name);
        this.account = account;
        this.drawingMoney = drawingMoney;
    }



    @Override
    public void run() {
        // 判断有没有钱
        if (account.money-drawingMoney < 0){
            System.out.println(Thread.currentThread().getName()+"钱不够，取不了");
            return;
        }

        // 模拟延时
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 卡内余额
        account.money = account.money-drawingMoney;
        // 手里的钱
        nowMoney = nowMoney + drawingMoney;

        System.out.println(account.name+"余额为"+account.money);
        System.out.println(this.getName()+"手里的钱为"+nowMoney);

    }

}